/*******************************************************************************
*  Project      : Minigate Golfin                                 
*  Program ID   : pop_waiting_app_ok.js
*  Description  : 웨이팅 확정 확인
*
********************************************************************************
*  Program History                                                            
*  Date        Author    Description                                          
*  ----------  --------  -------------------------------------------------------- 
*  2012-07-30  김효순    Created.  
*******************************************************************************/

jQuery(function($){
	// 예약정보
	function clsReserveList()
	{
		this._HTMLDom = null;
		this.listData = null;
		
		this._init();
	}
	
	clsReserveList.prototype = {
		_init: function(){
			var $layer = $("body");
			this._HTMLDom = {
				"$layer": $layer
				, "$element": $layer.find("div.pw_area02:first").clone(true)
			}
			
			this.setList([]);
		}
		, $getLayer: function(){
			return this._HTMLDom["$layer"];
		}
		, setList: function(thisData){
			this.listData = thisData;
			this.display();
		}
		, display: function(){
			var $layer = this.$getLayer();
			var $element = this._HTMLDom["$element"];
			var thisClass = this;
			
			$layer.find("div.pw_area02").remove();
			for(var i=0;i<this.listData.length;i++){
				$layer.append($element.clone(true));
			}
			$layer.find("div.pw_area02").each(function(n){
				var d = thisClass.listData[n];
				$(this).find("p:eq(0) strong:first").html(d["날짜"] + "(" + d["요일"]+ ")<br>" + d["골프장명"]+ "  " + d["코스명"] + " " + d["시간"] + "<br>");
			});
			
			_util.setWindowSize();
		}
		, spinStart: function(){
			var $layer = this.$getLayer();
			
			$layer.find("div.pw_area02").hide();
			if($layer.find("div#tmpSpin").length == 0){
				$layer.append("<div id='tmpSpin' style='position:relative; width:100%; height:100px;' />");
			}
			$layer.find("div#tmpSpin").show().spin();
		}
		, spinEnd: function(){
			var $layer = this.$getLayer();
			
			$layer.find("div.pw_area02").show();
			$layer.find("div#tmpSpin").hide().spin(false);
		}
	}
	
	function clsComm()
	{
		
		
		this._init();
	}
	
	clsComm.prototype = {
		_init: function(){
			
		}
		// 예약 리스트
		, getReserveList: function(arg){
			var thisClass = this;
			
			arg = arg || {};
			arg["콜백"] = arg["콜백"] || function(){};
			
			//ajax
			var serviceName = "";
			var options = {
				
			}
			_gf.ajax({
				
			}, options, function(ret){
				var res = {
					"결과코드": ret["msp"]["contents"]["serviceResult"]
					, "메시지": _gf.getResultMsg(ret)
					, "결과값": []
				}
				
				var items = ret["msp"]["contents"]["items"];
				if(items){
					if(!items.length) items = new Array(items);
					for(var i=0;i<items.length;i++){
						var obj = items[i];
						
						res["결과값"].push({
							"예약코드": _util.nvl(obj["예약코드"])
							, "지역코드": _util.nvl(obj["지역코드"])
							, "골프장코드": _util.nvl(obj["골프장코드"])
							, "코스코드": _util.nvl(obj["코스코드"])
							, "날짜": _util.nvl(obj["날짜"])
							, "시간": _util.nvl(obj["시간"])
						});
					}
				}
				
				arg["콜백"].apply(null, [res]);
			});
		}
	}
	
	
	// 컨트롤
	function clsControl()
	{
		this.objComm = null;
		this.objReserveList = null;
		
		this.reserveKey = null;
		
		this.openerControl = null;
	}
	
	clsControl.prototype = {
		_init: function(){
			this._preload();
			
			this.objComm = new clsComm();
			this.objReserveList = new clsReserveList();
			
			this._bind();
			this._beforeStart();
		}
		, _preload: function(){
			_gf.spinStart();
			
			this.reserveKey = _util.getParam("q_seq");
			
			if(opener){
				if(opener.document){
					this.openerControl = opener.document.objControl;
				}
			}
			
			if(!this.openerControl || !this.reserveKey){
				self.close();
				return;
			}
		}
		, _beforeStart: function(){
			_gf.spinEnd();
			this._start();
		}
		, _bind: function(){
			var thisClass = this;
			
			// 확인
			this.objReserveInfo.$getLayer().on("click", "img[alt='확인']", function(){
				thisClass.openerControl.closeWaitingApply(self, thisClass.objReserveInfo.getReserveInfo());
				self.close();
			});
			
			// 닫기
			$(document).on("click", "img[alt='취소'],img[alt='close']", function(){
				self.close();
			});
		}
		, _start: function(){
			var thisClass = this;
			
			this.objReserveList.spinStart();
			this.objComm.getReserveList({
				"콜백": function(ret){
					thisClass.finishReserveList(ret);
				}
			});
		}
		// 예약리스트 완료
		, finishReserveList: function(ret){
			if(ret["메시지"]) alert(ret["메시지"]);
			if(ret["결과코드"] != "00000") return;
			
			this.objReserveList.setList(ret["결과값"]);
		}
	}
	
	document.objControl = new clsControl();
	//document.objControl._init();
});